package top.lixunda.ecommerce.server.good.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import top.lixunda.common.utils.ObjectValidators;
import top.lixunda.ecommerce.server.business.BaseConvertServiceImpl;
import top.lixunda.ecommerce.server.business.api.entity.dto.PageConvertWrapperDTO;
import top.lixunda.ecommerce.server.business.api.entity.cache.CacheStore;
import top.lixunda.ecommerce.server.good.api.entity.vo.GoodConvertVO;
import top.lixunda.ecommerce.server.good.api.entity.db.GoodDetailDO;
import top.lixunda.ecommerce.server.good.api.entity.db.GoodsDO;
import top.lixunda.ecommerce.server.business.api.entity.dto.PageConvertDTO;
import top.lixunda.ecommerce.server.good.api.entity.db.SubGoodDO;
import top.lixunda.ecommerce.server.good.api.entity.dto.GoodDetailQueryDTO;
import top.lixunda.ecommerce.server.good.api.entity.dto.StoreRelatedProductDTO;
import top.lixunda.ecommerce.server.good.mapper.GoodDetailMapper;
import top.lixunda.ecommerce.server.good.mapper.GoodsMapper;
import top.lixunda.ecommerce.server.good.service.IGoodDetailService;

/**
 * <p>
 * VIEW 服务实现类
 * </p>
 *
 * @author Xumda
 * @since 2019-12-19
 */
@Service
public class GoodDetailServiceImpl
        extends BaseConvertServiceImpl<GoodDetailMapper, GoodDetailDO, GoodsServiceImpl,
        GoodsMapper, GoodsDO, GoodConvertVO>
        implements IGoodDetailService {

    @Autowired
    public GoodDetailServiceImpl() {
        super(GoodConvertVO::fromGoodDetail, GoodsDO::getId, GoodDetailDO::getGoodId);
    }

    /**
     * 根据id查询商品详情
     *
     * @param id 商品id
     * @return 商品详情
     */
    @Override
    public GoodConvertVO queryById(Integer id) {
        return this.getOneConvert(new LambdaQueryWrapper<GoodDetailDO>()
                .eq(GoodDetailDO::getGoodId, id)
                .eq(GoodDetailDO::getGoodState, GoodDetailDO.GOOD_STATE_NORMAL)
                .eq(GoodDetailDO::getSubState, GoodDetailDO.SUB_GOOD_STATE_NORMAL));
    }

    /**
     * 条件分页查询内容
     *
     * @param pageDTO 查询条件DTO, 包含查询条件，查询条件可以为空，为空时无条件进行查询
     * @return 分页数据
     */
    @Override
    public IPage<GoodConvertVO> queryGoodDetailByPage(
            PageConvertDTO<GoodsDO, GoodDetailDO, GoodDetailQueryDTO> pageDTO) {
        PageConvertWrapperDTO<GoodsDO, GoodDetailDO, GoodConvertVO> pageConvertWrapperDTO =
                new PageConvertWrapperDTO<GoodsDO, GoodDetailDO, GoodConvertVO>()
                        .setPage(pageDTO.getPage())
                        .setPageWrapper(pageDTO.getPageWrapper(
                                wrapper -> wrapper
                                        .eq(GoodsDO::getState, GoodsDO.STATE_NORMAL)
                        ))
                        .setQueryWrapper(pageDTO.getQueryWrapper(
                                wrapper -> wrapper
                                        .eq(GoodDetailDO::getGoodState, GoodDetailDO.GOOD_STATE_NORMAL)
                                        .eq(GoodDetailDO::getSubState, GoodDetailDO.SUB_GOOD_STATE_NORMAL)
                        ));
        return this.pageConvert(pageConvertWrapperDTO);
    }

    /**
     * 店家调用，根据id获取商品数据,只能查询本店铺的所有商品信息
     *
     * @param id 商品id
     * @return 商品列表
     */
    @Override
    public GoodConvertVO queryByIdStore(Integer id) {
        CacheStore cacheStore = currentStoreCacheNotNull();
        return this.getOneConvert(new LambdaQueryWrapper<GoodDetailDO>()
                .eq(GoodDetailDO::getGoodId, id)
                .eq(GoodDetailDO::getStoreId, cacheStore.getId()));
    }

    /**
     * 店家调用，根据条件筛选商品信息，只能查询本店铺的所有商品信息
     *
     * @param pageDTO 查询条件DTO, 包含查询条件，查询条件可以为空，为空时无条件进行查询
     * @return 商品列表
     */
    @Override
    public IPage<GoodConvertVO> queryGoodDetailByPageStore(
            PageConvertDTO<GoodsDO, GoodDetailDO, GoodDetailQueryDTO> pageDTO) {
        CacheStore cacheStore = currentStoreCacheNotNull();
        PageConvertWrapperDTO<GoodsDO, GoodDetailDO, GoodConvertVO> pageConvertWrapperDTO =
                new PageConvertWrapperDTO<GoodsDO, GoodDetailDO, GoodConvertVO>()
                        .setPage(pageDTO.getPage())
                        .setPageWrapper(pageDTO.getPageWrapper(
                                wrapper -> wrapper
                                        .eq(GoodsDO::getStoreId, cacheStore.getId())
                        ))
                        .setQueryWrapper(pageDTO.getQueryWrapper(
                                wrapper -> wrapper
                                        .eq(GoodDetailDO::getStoreId, cacheStore.getId())
                        ));
        return this.pageConvert(pageConvertWrapperDTO);
    }

    /**
     * 后台管理员调用，根据id获取商品数据,查询所有商品信息
     *
     * @param id 商品id
     * @return 商品列表
     */
    @Override
    public GoodConvertVO queryByIdAdmin(Integer id) {
        return this.getOneConvert(new LambdaQueryWrapper<GoodDetailDO>()
                .eq(GoodDetailDO::getGoodId, id));
    }

    /**
     * 后台管理员调用，根据条件筛选商品信息，查询所有商品信息
     *
     * @param pageDTO 查询条件DTO, 包含查询条件，查询条件可以为空，为空时无条件进行查询
     * @return 商品列表
     */
    @Override
    public IPage<GoodConvertVO> queryGoodDetailByPageAdmin(
            PageConvertDTO<GoodsDO, GoodDetailDO, GoodDetailQueryDTO> pageDTO) {
        PageConvertWrapperDTO<GoodsDO, GoodDetailDO, GoodConvertVO> pageConvertWrapperDTO =
                new PageConvertWrapperDTO<GoodsDO, GoodDetailDO, GoodConvertVO>()
                        .setPage(pageDTO.getPage())
                        .setPageWrapper(pageDTO.getPageWrapper())
                        .setQueryWrapper(pageDTO.getQueryWrapper());
        return this.pageConvert(pageConvertWrapperDTO);
    }

    /**
     * 查询新商品，分页查询，不包含条件，用户端调用
     *
     * @param pageDTO 分页数据
     * @return 商品列表
     */
    @Override
    public IPage<GoodConvertVO> queryNewProductsPage(
            PageConvertDTO<GoodsDO, GoodDetailDO, ?> pageDTO) {
        PageConvertWrapperDTO<GoodsDO, GoodDetailDO, GoodConvertVO> wrapper =
                new PageConvertWrapperDTO<>(pageDTO.getPage(),
                        new LambdaQueryWrapper<GoodsDO>()
                                .eq(GoodsDO::getState, GoodsDO.STATE_NORMAL),
                        new LambdaQueryWrapper<GoodDetailDO>()
                                .eq(GoodDetailDO::getSubState, SubGoodDO.STATE_NORMAL)
                                .orderByDesc(GoodDetailDO::getPushGoodTime));
        return this.pageConvert(wrapper);
    }

    /**
     * 查询某个店铺中的最新的商品的商品，用户端调用，不包含条件
     *
     * @param pageDTO 分页数据，不包含条件
     * @return 商品列表
     */
    @Override
    public IPage<GoodConvertVO> queryStoreRelatedProductPage(
            PageConvertDTO<GoodsDO, GoodDetailDO, StoreRelatedProductDTO> pageDTO) {
        if (ObjectValidators.isEmpty(pageDTO)) {
            throw getAppException("参数不能为空");
        }
        PageConvertWrapperDTO<GoodsDO, GoodDetailDO, GoodConvertVO> wrapper =
                new PageConvertWrapperDTO<>(pageDTO.getPage(),
                        pageDTO.getPageWrapper(w -> w.eq(GoodsDO::getState, GoodsDO.STATE_NORMAL)),
                        new LambdaQueryWrapper<GoodDetailDO>()
                                .eq(GoodDetailDO::getSubState, SubGoodDO.STATE_NORMAL)
                                .orderByDesc(GoodDetailDO::getPushGoodTime));
        return this.pageConvert(wrapper);
    }

}
